<?php 
$id = $_GET['id'];
if($id == 0)
redirect('index.php','',0);

$bets = safe_query("SELECT b.betID, u.username, b.money, b.date, b.itemID FROM bets AS b INNER JOIN user AS u ON b.user = u.userID where b.itemID = '".$id."' ORDER BY b.date DESC LIMIT 0, 5");

$bet_item = mysql_fetch_array(safe_query("SELECT price FROM item WHERE id='".$id."'"));
$money_pot = $bet_item['price'];

$cat = mysql_fetch_array(safe_query("SELECT category FROM item WHERE id='".$id."'"));
$category = mysql_fetch_array(safe_query("SELECT name FROM category WHERE id='".$cat['category']."'"));
$category = $category['name'];

$total_bets = mysql_num_rows(safe_query("SELECT betID FROM bets WHERE itemID='".$id."'"));
?>

<script type="text/javascript">
<!--
$(document).ready(function () {

		
	$(".single-bet").click(function () {
	var form_data = {
			item: $("#item").val(),
			user: $("#user").val(),
			money: 5
		};
		
		
	$.ajax({
			type: "POST",
			url: "?p=new_bet",
			data: form_data,
			success: function()
			{
			
			}
			
			});
	});
	$(".double-bet").click(function () {
	var form_data = {
			item: $("#item").val(),
			user: $("#user").val(),
			money: 10
		};
		
		
	$.ajax({
			type: "POST",
			url: "?p=new_bet",
			data: form_data,
			success: function()
			{
			
			}
			
			});
	});
	$(".triple-bet").click(function () {
	var form_data = {
			item: $("#item").val(),
			user: $("#user").val(),
			money: 15
		};
		
		
	$.ajax({
			type: "POST",
			url: "?p=new_bet",
			data: form_data,
			success: function()
			{
			
			}
			
			});
	});
	
	$("div.col-2 .place-bet ul").css("display", "none");
	$("div.col-2 .place-bet").click(function () {
		if ($(this).hasClass("active")) {
			$(this).removeClass("active");
			$("div.col-2 .place-bet ul").css("display", "none");
		} else {
			$("div.col-2 .place-bet ul").show();
			$(this).addClass("active");
		}
	});
	setInterval(function() {
		$.ajax({
			type: "POST",
			url: "money_pot",
			data: {item:$("#item").val() },
			dataType: "text",
			success: function(response)
			{
				$("#money-pot").html(response);
			
			}
		});
	}, 2000);
	
	setInterval(function() {
		$.ajax({
			type: "POST",
			url: "bet_count",
			data: {item:$("#item").val() },
			dataType: "text",
			success: function(response)
			{
				$("#betcount").html('<strong>Total bets:</strong> ' + response);
			
			}
		});
	}, 2000);

	setInterval(function() {
		$.ajax({
			type: "POST",
			url: "ajax_bets",
			data: {item:$("#item").val(), i2:"22" },
			dataType: "json",
			success: function(response)
			{
				$.each(response, function(bet, item) {
					var exists = false;
					$.each($(".bets-listing li"), function () {
						if ($(this).find("span").html() == bet) {
							exists = true;
						}
					});
					
			        if (!exists) {

			        	$(".bets-listing").prepend(item);
			        	$(".bets-listing li").eq(0).hide().show("blind", {percent: 100}, 600);
			        	if ($(".bets-listing li").length > 5) {
			        		$(".bets-listing li").eq(-1).hide("blind", {percent: 100}, 600);
			        	}
			        	setTimeout(function() {
			        		if ($(".bets-listing li").length > 5) {
				        		$(".bets-listing li").eq(-1).remove();
			        		}
			        		$(".bets-listing li").removeClass("last");
			        		$(".bets-listing li").eq(-1).addClass("last");
				        }, 500);
			        }




			        //Example

			        /*if (bet == 2) {


			        	$(".bets-listing").prepend(item);
			        	$(".bets-listing li").eq(0).hide().show("blind", {percent: 100}, 600);
			        	if ($(".bets-listing li").length > 5) {
			        		$(".bets-listing li").eq(-1).hide("blind", {percent: 100}, 600);
			        	}


			        	setTimeout(function() {
			        		if ($(".bets-listing li").length > 5) {

				        		$(".bets-listing li").eq(-1).remove();
			        		}
			        		$(".bets-listing li").removeClass("last");
			        		$(".bets-listing li").eq(-1).addClass("last");

				        }, 500);


				   	}*/


			    });
			}
		});
	}, 2000);
});
//-->

</script>
<script src="js/jquery.countdown.js"></script>
<script src="js/countdown.js"></script>

<div id="betspage">
	<h1 class="heading-page">I will lorem ipsum dolor sit amet</h1>
    <div class="tail clearafter">
    	<span class="tail-left"></span>
        <span class="tail-right"></span>
        <span class="bets-cate left"><?=$category?></span>
        <span class="bets-money right" id="money-pot">$<?=$money_pot?></span>
        <div class="bets-rating">
        	<div class="process">
            	<div class="green"></div>
            </div>

            <div class="percent clearafter">
            	<span class="left">50%</span>
                -
                <span class="right">50%</span>
            </div>
        </div>
    </div>


    <div class="bets-content clearafter">
    	<div class="left col-1">
        	<div class="bets-image"><img src="images/bets-img.png" alt="bets img" title="bets img" /></div>
			
            <p class="description">
            	Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tortor lacus, congue sit amet ullamcorper ut, interdum nec ante. Nam vehicula eleifend enim eget sagittis. Ut sed velit augue. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dictum elementum odio non rutrum.
            </p>
        </div>


        <div class="left col-2">
        	<div class="place-bet">
            	<ul>
                	<li class="single-bet"><a href="#"></a></li>
                    <li class="double-bet"><a href="#"></a></li>
                    <li class="triple-bet"><a href="#"></a></li>
                </ul>
            </div>


            <div class="total-bet">
            	<div id="betcount"><strong>Total bets: </strong><?=$total_bets?></div>
                <div class="popular-country">
                	<div><strong>Popular country:</strong></div>
                    <div><span class="iconflag-sweden">Sweden</span></div>
                </div>
            </div>
        </div>


        <div class="left col-3">
			<ul class="bets-listing">
        		<?php
        		$count = 1;
        		$num_rows = mysql_num_rows($bets);
				if($num_rows < 5)
				{
				?>
				<li <?=$num_rows == 0 ? "class=\"last\"" : ""?> >
                	<span class="icon-yellow"><strong>emil</strong> created this bet</span>
                    <span class="bets-price">11 July</span>
                </li>
				<?php
				}
				while($bet = mysql_fetch_array( $bets )) 
				{				
				?>
            	<li class="<?=$count == $num_rows ? "last" : ""?> clearafter" >
            		<span class="id" style="display: none"><?=$bet["betID"]?></span>
                	<span class="<?=(time() - $bet["date"]) <= 60*10 ? "icon-green" : "icon-yellow"?>"><strong><?=$bet["username"]?></strong> <?=(time() - $bet["date"]) <= 60*10 ? "just" : "has"?> placed a bet</span>
					
                    <span class="bets-price">$<?=$bet["money"]?></span>
                </li>

                <?php
                	$count ++;
				}
                ?>
            </ul>
        </div>
    </div>


    <div class="bets-date-created clearafter">
    	<span class="left"><strong>Created 5 days ago</strong></span>
    	<span class="right" id="countdown"><strong>Time left:</strong> Undefined</span>
    </div>

</div>

<input type="hidden" id="item" value="<?=$id?>" />
<input type="hidden" id="user" value="<?=$userID?>" />